package com.bawei.monthdemo2207a.month01.home

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.ShoppingCart
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.monthdemo2207a.App
import com.bawei.monthdemo2207a.R
import com.bawei.monthdemo2207a.model.http.DownloadUtil
import com.bawei.monthdemo2207a.month01.route.RouteConfig
import com.king.ultraswiperefresh.UltraSwipeRefresh
import com.king.ultraswiperefresh.rememberUltraSwipeRefreshState
import kotlinx.coroutines.delay

/**
 * 作者：赵亮
 * 时间：2024/12/26 16:05
 * 功能：首页面
 */

data class HomeEntity(
    val img: Int,
    val logo: Int,
    val name: String,
    val score: String,
    val downloadCount: String,
    val info: String
)

@Composable
fun HomePage(navController: NavController = rememberNavController()) {

    var searchText by remember {
        mutableStateOf("抖音")
    }

    // 横向列表数据
    val list1 = listOf(
        HomeEntity(R.drawable.app_weixin, R.drawable.logo_weixin, "微双开分身", "3.7", "", ""),
        HomeEntity(R.drawable.app_weixin, R.drawable.logo_weixin, "万能遥控器", "3.2", "", ""),
        HomeEntity(R.drawable.app_weixin, R.drawable.logo_weixin, "微双开分身", "3.7", "", ""),
    )
    // 竖向列表数据
    val list = remember {
        SnapshotStateList<HomeEntity>()
    }
    LaunchedEffect(key1 = "") {
        list.addAll(initList2())
    }

    // 刷新状态
    val state = rememberUltraSwipeRefreshState()

    LaunchedEffect(state.isRefreshing) {
        if (state.isRefreshing) {
            delay(2000)
            list.clear()
            list.addAll(initList2())
            state.isRefreshing = false
        }
    }

    LaunchedEffect(state.isLoading) {
        if (state.isLoading) {
            delay(2000)
            list.addAll(initList2())
            state.isLoading = false
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(top = 24.dp)
            .background(color = colorResource(id = R.color.body_bg))
    ) {
        // 标题
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 15.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .weight(1f)
                    .background(
                        color = colorResource(id = R.color.search_bg),
                        shape = RoundedCornerShape(20.dp)
                    )
            ) {
                Icon(
                    modifier = Modifier.padding(10.dp),
                    imageVector = Icons.Default.Search,
                    contentDescription = null
                )
                BasicTextField(value = searchText, onValueChange = { searchText = it })
            }
            Icon(modifier = Modifier.clickable {
                // 点击跳转到本地模块
                navController.navigate(RouteConfig.LOCAL_FILE)
            }, imageVector = Icons.Default.ShoppingCart, contentDescription = null)
        }
        // 横向列表
        LazyRow(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
            items(list1) {
                Column(
                    modifier = Modifier.background(
                        color = Color.White,
                        shape = RoundedCornerShape(15.dp)
                    )
                ) {
                    Image(
                        modifier = Modifier
                            .size(170.dp, 80.dp)
                            .clip(RoundedCornerShape(topStart = 15.dp, topEnd = 15.dp)),
                        contentScale = ContentScale.Crop,
                        painter = painterResource(id = it.img),
                        contentDescription = null
                    )
                    Row(
                        modifier = Modifier.padding(start = 7.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Image(
                            modifier = Modifier
                                .size(30.dp)
                                .clip(RoundedCornerShape(7.dp)),
                            contentScale = ContentScale.Crop,
                            painter = painterResource(id = it.logo),
                            contentDescription = null
                        )
                        Column(modifier = Modifier.padding(start = 7.dp)) {
                            Text(text = it.name, fontSize = 10.sp)
                            Row {
                                Icon(
                                    modifier = Modifier.size(10.dp),
                                    imageVector = Icons.Default.Star,
                                    contentDescription = null
                                )
                                Text(text = it.score, fontSize = 8.sp)
                            }
                        }
                        Box(
                            modifier = Modifier
                                .size(50.dp, 20.dp)
                                .padding(start = 10.dp)
                                .background(Color.Green, shape = RoundedCornerShape(20.dp))
                        ) {
                            Text(text = "安装", modifier = Modifier.align(Alignment.Center))
                        }
                    }
                }
            }
        }
        // 竖向列表
        Box(
            Modifier
                .fillMaxSize()
                .padding(10.dp)
                .background(color = Color.White, shape = RoundedCornerShape(15.dp))
        ) {
            UltraSwipeRefresh(
                modifier = Modifier.fillMaxSize(),
                state = state,
                onRefresh = { state.isRefreshing = true },
                onLoadMore = { state.isLoading = true }) {
                LazyColumn(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(10.dp),
                    verticalArrangement = Arrangement.spacedBy(10.dp)
                ) {
                    items(list) {
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Image(
                                modifier = Modifier
                                    .size(45.dp)
                                    .clip(RoundedCornerShape(7.dp)),
                                contentScale = ContentScale.Crop,
                                painter = painterResource(id = it.logo),
                                contentDescription = null
                            )
                            Column(
                                modifier = Modifier
                                    .padding(start = 10.dp)
                                    .weight(1f)
                            ) {
                                Text(text = it.name)
                                Row {
                                    Icon(
                                        modifier = Modifier.size(10.dp),
                                        imageVector = Icons.Default.Star,
                                        contentDescription = null
                                    )
                                    Text(text = it.score, fontSize = 8.sp)
                                    Text(text = it.downloadCount, fontSize = 8.sp)
                                }
                                Text(text = it.info)

                            }
                            Box(
                                modifier = Modifier
                                    .size(50.dp, 20.dp)
                                    .padding(start = 10.dp)
                                    .background(Color.Green, shape = RoundedCornerShape(20.dp))
                            ) {
                                Text(
                                    text = "安装",
                                    modifier = Modifier
                                        .align(Alignment.Center)
                                        .clickable {
                                            DownloadUtil.download(
                                                App.context,
                                                "https://47ee78mh3302.vicp.fun/fileDownload?fileName=webview.apk",
                                                "${it.name}.apk"
                                            )
                                        })
                            }
                        }

                    }
                }
            }
        }
    }
}

private fun initList2() = listOf(
    HomeEntity(
        R.drawable.logo,
        R.drawable.logo_weixin,
        "快手",
        "3.7",
        "274亿次",
        "拥抱每一种生活·评论破十万"
    ),
    HomeEntity(
        R.drawable.logo,
        R.drawable.logo_weixin,
        "快手极速版",
        "3.2",
        "274亿次",
        "拥抱每一种生活·评论破十万"
    ),
    HomeEntity(
        R.drawable.logo,
        R.drawable.logo_weixin,
        "微双开分身",
        "3.7",
        "274亿次",
        "拥抱每一种生活·评论破十万"
    ),
    HomeEntity(
        R.drawable.logo,
        R.drawable.logo_weixin,
        "微双开分身",
        "3.7",
        "274亿次",
        "拥抱每一种生活·评论破十万"
    ),
    HomeEntity(
        R.drawable.logo,
        R.drawable.logo_weixin,
        "微双开分身",
        "3.7",
        "274亿次",
        "拥抱每一种生活·评论破十万"
    ),
    HomeEntity(
        R.drawable.logo,
        R.drawable.logo_weixin,
        "微双开分身",
        "3.7",
        "274亿次",
        "拥抱每一种生活·评论破十万"
    ),
)

@Preview(showSystemUi = true)
@Composable
fun HomePagePreview() {
    HomePage()
}

